<?php
namespace App\Service;

use App\Entity\Money;
use App\Entity\OrderPackage;
use App\Entity\Orders;
use App\Entity\Task;
use App\Repository\ConfigRepository;
use App\Repository\SalesUserJuanRepository;
use App\Repository\ShopTagRepository;
use App\Repository\TaskVoteRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;

class OrderService extends DocumentRepository
{

    public $shopTagRepository;
    public $configRepository;
    public $shopService;
    public $moneyService;
    public $announceService;
    public $commentService;
    public $taskVoteRepository;
    public $salesUserJuanRepository;


    public function __construct(
        DocumentManager $dm
        ,MoneyService $moneyService
        ,ConfigRepository $configRepository
        ,SalesUserJuanRepository $salesUserJuanRepository
    )
    {
//        parent::__construct($registry, Orders::class);
        parent::__construct($dm, $dm->getUnitOfWork(), $dm->getClassMetadata(Orders::class));
        $this->configRepository = $configRepository;
        $this->moneyService = $moneyService;
        $this->salesUserJuanRepository = $salesUserJuanRepository;
    }


    public function dealWithOrderPay($orderId)
    {
        $order = $this->find($orderId);
        if (!$order) {
            die();
        }
        /**@var $order \App\Entity\Orders */
        $order->setStatus(1)->setPayAt(new \DateTime());
        $this->dm->persist($order);
        $this->dm->flush();
        //奖励用户积分
        $payReal = $order->getPayReal();
        $rate = $this->configRepository->getByK("JIFEN_PERCENT");

        $jifen = floor($rate * $payReal);
        $this->moneyService->mod($order->getUser(),$jifen,MoneyService::__MONEY_JI_FEN__,102,"加油奖励",["orderId"=>$order->getId()]);
        //修改劵状态
        $juan = $this->salesUserJuanRepository->find(  $order->getInfo()["juan"]??0 );
        if ($juan) {
            $juan->setStatus(2);
            $this->dm->persist($juan);
            $this->dm->flush();
        }
        //向管理员发通知
        $jpush = new Jpush();
        $jpush->push();

        return true;
    }
}